package com.yanqu.road.server.cmd.union.cross.trade;

import com.yanqu.road.entity.union.trade.UnionTradeLog;
import com.yanqu.road.entity.union.trade.UnionTradeTeamData;
import com.yanqu.road.logic.pb.CrossUnionPb;
import com.yanqu.road.pb.union.UnionTradeProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionReqContext;
import com.yanqu.road.server.manager.union.trade.CrossUnionTradeMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.C_UNION_TRADE_ROUTE_DATA_LOG_LIST, desc = "路线日志")
public class CrossTradeLogDataListCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        UnionTradeProto.TradeLogDataListReqMsg reqMsg = UnionTradeProto.TradeLogDataListReqMsg.parseFrom(packet.getBytes());
        int routeId = reqMsg.getRouteId();

        CrossUnionReqContext context = CrossUnionReqContext.initContext(serverId, packet.getPlayerId()).loadUnionMember().loadUnionInfo();

        UnionTradeProto.TradeLogDataListRespMsg.Builder respMsg = UnionTradeProto.TradeLogDataListRespMsg.newBuilder();
        if(context.getRet() != 0){
            ret = context.getRet();
        }else {
            UnionTradeTeamData unionTradeTeamData = CrossUnionTradeMgr.getTeamDataByUnionUinAndRouteId(context.getUnionInfo().getUnionUid(), routeId);
            if(unionTradeTeamData != null){
                List<UnionTradeLog> unionTradeLogList = unionTradeTeamData.getNewLogDataList();
                respMsg.addAllLogDataList(CrossUnionPb.parseTradeLogDataTempMsgList(unionTradeLogList));
            }
        }
        respMsg.setRet(ret);

        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_TRADE_ROUTE_LOG_DATA_LIST, respMsg));

    }

    @Override
    public void sendData() {

    }
}
